/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farmcode.plugin.objectinfo.jdbc;

import com.farmcode.api.runtime.FARMCodeContext;
import com.farmcode.api.runtime.metadata.Table;
import com.farmcode.api.runtime.metadata.View;
import com.farmcode.api.runtime.metadata.ViewSpc;
import com.farmcode.api.runtime.metadata.tables.Attribute;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author oiprado
 */
public class ViewsDatabaseObjectInfo extends DatabaseInfo<View, View> {

  private String[] types = {"VIEW"};
  private java.sql.DatabaseMetaData databaseMetaData = null;
  
  public ViewsDatabaseObjectInfo(FARMCodeContext context) throws ClassNotFoundException {
    super(context);
  }
  
  @Override
  public List<View> explore() throws SQLException {
    List<View> tables = new ArrayList<View>();

    String schema = (String) context.get(FARMCodeContext.SCHEMA);
    Connection connection = getConnection();
    databaseMetaData = connection.getMetaData();
    ResultSet resultSet = databaseMetaData.getTables(null, (schema).equals("") ? null : schema, null, types);
    while (resultSet.next()) {
      View dbo = new ViewSpc();
      String tableName = resultSet.getString(3); // table name (3)
      String tableType = resultSet.getString(4); // table type (4)
      if (!tableName.contains("$")) {
        dbo.setName(tableName);
        dbo.setType(tableType);
        tables.add(dbo);
      }
    }
    resultSet.close();
    return tables;
  }

  @Override
  public View exploreChilds(View dbo) throws SQLException {
    String schema = (String) context.get(FARMCodeContext.SCHEMA);
    Connection connection = getConnection();
    databaseMetaData = connection.getMetaData();
    ResultSet resultSet = databaseMetaData.getColumns(null, schema == null ? "" : schema, dbo.getName(), null);
    
    while (resultSet.next()) {
      String columnName = resultSet.getString("COLUMN_NAME");
      String type = resultSet.getString("TYPE_NAME");
      Integer dataType = resultSet.getInt("DATA_TYPE");
      String precision = resultSet.getString("COLUMN_SIZE");
      String isNullable = resultSet.getString("IS_NULLABLE");
      String scala = resultSet.getString("DECIMAL_DIGITS");
      String remarks = resultSet.getString("REMARKS");
      
      Attribute attribute = new Attribute();
//      attribute.put("name", columnName);
//      attribute.put("type", type);
//      attribute.put("precision", precision);
//      attribute.put("identity", "" + type.contains("identity"));
//      attribute.put("guid", "" + columnName.toLowerCase().contains("guid"));
//      attribute.put("pk", "false");
//      attribute.put("fk", "false");
//      attribute.put("scala", scala == null ? "0" : scala);
//      attribute.put("fktable", "none");
//      attribute.put("exptable", "none");
//      attribute.put("remarks", remarks);
//      attribute.put("native", getTypes().get(dataType).getNativeType());
//      attribute.put("datatype",  getTypes().get(dataType).getName());
//      attribute.put("nullable", isNullable.equals("YES") ? "true" : "false");
      dbo.getAttributeChilds().add(attribute);

    }
    return dbo;
  }  

//  private void lookUpProperty(Table dbo, String name, String key, String value) {
//
//    int length = dbo.getAttributeChilds().size();
//    for (int index = 0; index < length; index++) {
//      Attribute attribute = (Attribute) dbo.getAttributeChilds().get(index);
//      if (attribute.get("name").equals(name)) {
//        attribute.put(key, value);
//        break;
//      }
//    }
//  }
  
}
